<?php
namespace App\Http\Sem\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;


class InputController  extends Controller
{

        public function insert_input_jiali(Request $request){
            header('content-type:application:json;charset=utf8');
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Methods:GET');
            header('Access-Control-Allow-Headers:x-requested-with,content-type');


            date_default_timezone_set('PRC');
            $data = $request->only("phone","status");

            //验证
            if(!preg_match('/^[0-9]{1,1}$/',$data['status']))  return 42112;
            if(!preg_match('/^1[34578]\d{9}$/',$data['phone']))  return 42113;

            $data['ip']=getip();

            $data['created_time']=date("Y-m-d H:i:s",time());

            $phoneCount = DB::table("SEM_input")->where("phone",$data['phone'])->count();
            //电话号码重复
            if($phoneCount > 0 ) return 14235;
            //当前ip在同一时间 提交次数过多
            $wips = DB::table("SEM_input")
                ->where([
                    ['ip','=',$data['ip']],
                    ['created_time',">",date("Y-m-d H:i:s",strtotime("-1 day"))]
                ])
                ->count();
            if($wips > 3){
                return 402;
            }
            //条件信息

            $res = DB::table("SEM_input")->insertGetId($data);

            if($res){
                $this->mail($data['phone']);
                return 200;
            }else{
                return 401;
            }


        }



        public function mail($phone){
            $message = '$phone';
            $to = 'lsnjlgzh@163.com';
//            $subject = '邮件名称';
            Mail::send(
                'welcome',
                ['content' => $message],
                function ($message) use($to, $phone) {
                    $message->to($to)->subject($phone);
                }
            );
        }


    public function insert_input_jiaji(Request $request){
        header('content-type:application:json;charset=utf8');
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');


        date_default_timezone_set('PRC');
        $data = $request->only("phone","status");

        //验证
        if(!preg_match('/^[0-9]{1,1}$/',$data['status']))  return 42112;
        if(!preg_match('/^1[34578]\d{9}$/',$data['phone']))  return 42113;

        $data['ip']=getip();

        $data['created_time']=date("Y-m-d H:i:s",time());

        $phoneCount = DB::table("SEM_input")->where("phone",$data['phone'])->count();
        //电话号码重复
        if($phoneCount > 0 ) return 14235;
        //当前ip在同一时间 提交次数过多
        $wips = DB::table("SEM_input")
            ->where([
                ['ip','=',$data['ip']],
                ['created_time',">",date("Y-m-d H:i:s",strtotime("-1 day"))]
            ])
            ->count();
        if($wips > 3){
            return 402;
        }
        //条件信息

        $res = DB::table("SEM_input")->insertGetId($data);

        if($res){
            $this->mail2($data['phone']);
            return 200;
        }else{
            return 401;
        }


    }


    public function insert_shichang_input1(Request $request){
        header('content-type:application:json;charset=utf8');
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        
        

        $data = $request->only("name",'phone','wechat','city','content');
        $data ['ip']=$request->getClientIp();
        $data['created_time']=date("Y-m-d H:i:s",time());
        $res = DB::connection('mysql3')->table('from')->insertGetId($data);
        // $res = DB::connection('mysql3')->table('from')->get();
        return 200;
    }



    public function mail2($phone){
        $message = '$phone';
        $to = 'jiajilaw@163.com';
//            $subject = '邮件名称';
        Mail::send(
            'welcome',
            ['content' => $message],
            function ($message) use($to, $phone) {
                $message->to($to)->subject($phone);
            }
        );
    }

}
